<template>
  <work-template v-loading="loading" v-model="ruleForm"
                 :flowId="flowId"
                 @closeSelectedTag="closeSelectedTag" :butShow="false">
    <div slot="form" class="container-div">
      <!--处置结果  合同信息 处置付款信息  违约信息 收款进度 付款进度-->
      <disposalResultMessage v-model="ruleForm" state="readonly" @reset="reset"/>
    </div>
  </work-template>
</template>

<script>
import * as apis from '@/api/vehicleDisposal/routineHandle.js'
import {getDisposalResult, isDisposeVehicleBilling} from "@/api/vehicleDisposal/routineHandle.js";

export default {
  name: 'DisposalResultDetails',
  components: {
    disposalResultMessage: () => import('../../components/routineDisposal/routineHandle/disposalResultMessage.vue')
  },
//处置结果详情
  computed: {
    flowId() {
      return this.$route.query?.flowId
    }
  },
  watch: {},
  data() {
    return {
      ruleForm: {},
      loading: false
    };
  },
  created() {
    if (this.$route.query.id) {
      this.handleDetails()
    }
  },
  mounted() {
  },
  methods: {
    async handleDetails() {
      this.loading = true
      try {
        const res = await getDisposalResult(this.$route.query.id)
        this.ruleForm = res.data
        for (let i = 0; i < this.ruleForm.financeProceedsVos.length; i++) {
          let obj = await this.handleBoole(this.ruleForm.financeProceedsVos[i])
          //开票状态
          this.$set(this.ruleForm.financeProceedsVos[i], 'ticketStatus', obj.ticketStatus)
          this.$set(this.ruleForm.financeProceedsVos[i], 'isTicketStatus', obj.isTicketStatus)
          //负数发票状态
          this.$set(this.ruleForm.financeProceedsVos[i], 'negativeNumberTicketStatus', obj.negativeNumberTicketStatus)
          this.$set(this.ruleForm.financeProceedsVos[i], 'showNegativeNumberTicketStatus', obj.showNegativeNumberTicketStatus)
          //发票id
          this.$set(this.ruleForm.financeProceedsVos[i], 'settleId', obj.settleId)
        }
      } catch (e) {
        console.error(e)
      } finally {
        this.loading = false
      }
    },

    async handleBoole(row) {
      if (!row.earnCode || !row.mainOrderCode) {
        return {
          ticketStatus: -1,
          isTicketStatus: -1,
          negativeNumberTicketStatus: -1,
          showNegativeNumberTicketStatus: -1
        };
      }

      const obj = {
        businessCode: row.earnCode,
        mainOrderCode: row.mainOrderCode
      };
      try {
        const response = await isDisposeVehicleBilling(obj);
        return response.data;
      } catch (error) {
        return {
          ticketStatus: -1,
          isTicketStatus: -1,
          negativeNumberTicketStatus: -1,
          showNegativeNumberTicketStatus: -1
        };
      }
    },

    reset() {
      this.handleDetails()
    },

    closeSelectedTag(view) {
      this.$tab.closePage(view).then(({visitedViews}) => {
        this.$router.push({
          path: "/VehicleDisposal/RoutineDisposal/RoutineHandleDetails",
          query: {
            id: this.$route.query.id,
            type: 'view',
            path: 'RoutineHandleList',
            vehicleDisposalNo: this.$route.query.vehicleDisposalNo,
          }
        });
      });
    },
  },
};
</script>

<style lang="scss" scoped>
@import "@/styles/cwgd/container.scss";
</style>


